package consumer;

import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import service.ActiveMQService;
import service.ImageService;

public class ImageConsumer implements ServletContextListener, MessageListener,
		ExceptionListener {

	private static ImageService imgService = new ImageService();
	@Override
	public void onMessage(Message message) {		
		TextMessage textMessage = (TextMessage) message;		
		try {
			String imgURL = textMessage.getText();
			imgService.getImageFromUrl(imgURL);
			
		} catch (JMSException e) { 
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void onException(JMSException arg0) {

	}

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {

	}

	public void getMessage() throws JMSException, InterruptedException {
		ActiveMQService.start("imageSMS");
		MessageConsumer consumer = ActiveMQService.session
				.createConsumer(ActiveMQService.destination);
		ActiveMQService.connection.setExceptionListener(this);
		consumer.setMessageListener(this);
	}
}
